﻿// ActionScript file
package org.nickro.effects{
	import caurina.transitions.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class MaskEffect extends MovieClip{
		public static const COMPLETE:String="complete";
		private var _duration:Number;
		private var _direction:String;
		private var _delay:Number=0;
		private var _transition:String="linear";
		private var _content:*;
		//private var _easing
		private var bitmap:Bitmap;
		private var bitmapData:BitmapData;
		public function MaskEffect(content:*,params:Object){
			if(content is Class){
				_content=new content;
			}
			if(content is MovieClip){
				_content=content as MovieClip;
			}
			else{
				_content=content;
			}
			addChild(_content);
			_duration=params.duration;
			_direction=params.direction;
			_delay=params.delay;
			_transition=params.transition;
			createRect();
		} 
		public function start():void{
			maskMove()
		}
		public function restore():void{
			maskRestore();
		}
		private function createRect():void{
			var rect:Rectangle;
			rect=new Rectangle(0,0,_content.width,_content.height);
			bitmapData=new BitmapData(_content.width,_content.height,false,0x000000);
			bitmap=new Bitmap(bitmapData);
			addChild(bitmap);
			_content.mask=bitmap;
		}
		private function maskMove():void{
			switch(_direction){
				case "up":
					Tweener.addTween(bitmap,{y:-_content.height,time:_duration,delay:_delay,transition:_transition,onComplete:completeHandler});
					break;
				case "down":
					Tweener.addTween(bitmap,{y:_content.height,time:_duration,delay:_delay,transition:_transition,onComplete:completeHandler});
					break;
				case "left":
					Tweener.addTween(bitmap,{x:-_content.width,time:_duration,delay:_delay,transition:_transition,onComplete:completeHandler});
					break;
				case "right":
					Tweener.addTween(bitmap,{x:_content.width,time:_duration,delay:_delay,transition:_transition,onComplete:completeHandler});
					break;
			}
		}
		private function maskRestore():void{
			Tweener.addTween(bitmap,{x:0,y:0,time:_duration,delay:_delay,transition:_transition});
		}
		private function completeHandler():void{
			dispatchEvent(new Event(MaskEffect.COMPLETE));
		}
	}
}